package com.atguigu.myjuc.mythread.day2;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

//100个线程同时进行多次读写 list set map
public class HundredThreadReadWrite {
    public static void listNoSafe(){

        //List list=new ArrayList<>();
        // List list= Collections.synchronizedList(new ArrayList<>());
        List list=new CopyOnWriteArrayList();

        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                list.add (UUID.randomUUID().toString().substring(0,6));


                System.out.println(list);
            },"i").start();
        }
    }
    public static void setNoSafe(){

         //Set<String> set=new HashSet<>();
         Set<String> set=Collections.synchronizedSet(new HashSet<>());
         //Set<String> set=new CopyOnWriteArraySet<>();

        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                set.add (UUID.randomUUID().toString().substring(0,6));


                System.out.println(set);
            },"i").start();
        }
    }
    public static void mapNoSafe(){

        //Map<String,String> map=new HashMap<>();
        //Map<String,String> map=Collections.synchronizedMap(new HashMap<>());
        Map<String,String> map=new ConcurrentHashMap<>();
         new HashMap<>().put(1,2);
        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                map.put (Thread.currentThread().getName(),UUID.randomUUID().toString().substring(0,6));


                System.out.println(map);
            },String.valueOf(i)).start();
        }
    }
    public static void main(String[] args) {
        mapNoSafe();
    }




}
